package com.example.test2.db.control;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.test2.db.entity.Clothes;
import com.example.test2.db.gen.ClothesDao;
import com.example.test2.db.gen.DaoMaster;
import com.example.test2.db.gen.DaoSession;

import java.util.List;

public class ClothesDbController {
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private Context context;
    private ClothesDao clothesDao;
    private static ClothesDbController mDbController;
    public static ClothesDbController getInstance(Context context){
        if(mDbController == null){
            synchronized (ClothesDbController.class){
                if(mDbController == null){
                    mDbController = new ClothesDbController(context);
                }
            }
        }
        return mDbController;
    }
    public ClothesDbController(Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context,"clothes.db", null);
        mDaoMaster =new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        clothesDao = mDaoSession.getClothesDao();
    }
    private SQLiteDatabase getReadableDatabase(){
        if(mHelper == null){
            mHelper = new DaoMaster.DevOpenHelper(context,"clothes.db",null);
        }
        SQLiteDatabase db =mHelper.getReadableDatabase();
        return db;
    }
    private SQLiteDatabase getWritableDatabase(){
        if(mHelper == null){
            mHelper =new DaoMaster.DevOpenHelper(context,"clothes.db",null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }
    public void insertOrReplace(Clothes video){
        clothesDao.insertOrReplace(video);
    }
    public List<Clothes> searchByName(String name){
        List<Clothes> video = clothesDao.queryBuilder().where(ClothesDao.Properties.Name.like("%"+name+"%")).list();
        return video;

    }
    public List<Clothes> searchByType(String type){
        List<Clothes>videos = clothesDao.queryBuilder().where(ClothesDao.Properties.Type.eq(type)).list();
        return videos;
    }
    public List<Clothes> searchByShopName(String name){
        List<Clothes> video = clothesDao.queryBuilder().where(ClothesDao.Properties.ShopName.like("%"+name+"%")).list();
        return video;
    }
    public List<Clothes> searchAll(){
        List<Clothes>videos=clothesDao.queryBuilder().list();
        return videos;
    }

}
